package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements  RoleService {
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;
    @Autowired
    private MenuDao menuDao;
    @Override
    public List<Permission> findAllPermission() {
        return permissionDao.findAll();
    }

    @Override
    public List<Menu> findAllMenu() {
        return menuDao.findAll();
    }

    @Override
    public void add(Role role, Integer[] permissionIds, Integer[] menuIds) {
        //新增角色
        roleDao.add(role);
        Integer roleId = role.getId();//角色id

        //设置权限和角色多对多关系
        //设置多对多关系
        if(permissionIds != null && permissionIds.length > 0){
            for (Integer permissionId : permissionIds) {//角色id
                Map<String,Integer> map = new HashMap<>();
                map.put("role_id",roleId);
                map.put("permission_id",permissionId);
                roleDao.setPermissionAndRole(map);
            }
        }
        //设置菜单和角色多对多关系
        if(menuIds != null && menuIds.length > 0){
            for (Integer menuId : menuIds) {//角色id
                Map<String,Integer> map = new HashMap<>();
                map.put("role_id",roleId);
                map.put("menu_id",menuId);
                roleDao.setMenuAndRole(map);
            }
        }
    }

    @Override
    public PageResult findPage(QueryPageBean pageBean) {
        Integer currentPage = pageBean.getCurrentPage();
        Integer pageSize = pageBean.getPageSize();
        String queryString = pageBean.getQueryString();
        //使用分页助手插件实现分页查询
        PageHelper.startPage(currentPage,pageSize);
        Page<Role> page = roleDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public Role findById(Integer id) {
        return roleDao.findById(id);
    }

    @Override
    public List<Integer> findPermissionIdsByRoleId(Integer roleId) {
        return roleDao.findPermissionIdsByRoleId(roleId);
    }

    @Override
    public List<Integer> findMenuIdsByRoleId(Integer roleId) {
        return roleDao.findMenuIdsByRoleId(roleId);
    }

    @Override
    public void edit(Role role, Integer[] permissionIds, Integer[] menuIds) {
        //编辑角色
        roleDao.update(role);
        Integer roleId = role.getId();//角色id
        //删除权限和角色多对多关系
        roleDao.delPermissionAndRoleByRoleId(roleId);
        //删除菜单和角色多对多关系
        roleDao.delMenuAndRoleByRoleId(roleId);
        //重新设置权限和角色多对多关系
        if(permissionIds != null && permissionIds.length > 0){
            for (Integer permissionId : permissionIds) {//角色id
                Map<String,Integer> map = new HashMap<>();
                map.put("role_id",roleId);
                map.put("permission_id",permissionId);
                roleDao.setPermissionAndRole(map);
            }
        }
        //重新设置菜单和角色多对多关系
        if(menuIds != null && menuIds.length > 0){
            for (Integer menuId : menuIds) {//角色id
                Map<String,Integer> map = new HashMap<>();
                map.put("role_id",roleId);
                map.put("menu_id",menuId);
                roleDao.setMenuAndRole(map);
            }
        }
    }

    @Override
    public void delete(Integer id) {
        //删除权限和角色多对多关系
        roleDao.delPermissionAndRoleByRoleId(id);
        //删除菜单和角色多对多关系
        roleDao.delMenuAndRoleByRoleId(id);
        //删除角色
        roleDao.delete(id);
    }
}
